﻿using Microsoft.Extensions.DependencyInjection;
using System;

namespace Zo.IoC
{
    public class IoCManager
    {
        private IServiceProvider _serviceProvider = null;

        /// <summary>
        /// IoC 实例。
        /// </summary>
        public static readonly IoCManager Instance = new IoCManager();

        public void SetServiceProvider(IServiceProvider serviceProvider)
        {
            _serviceProvider = serviceProvider;
        }

        public T GetService<T>()
        {
            if (_serviceProvider == null)
            {
                throw new Exception("请在启动程序中注册IserviceProvider: IoCManager.Instance.SetServiceProvider(services.BuildServiceProvider());");
            }
         
            return _serviceProvider.GetService<T>();
        }
    }
}